package com.huawei.hmsdemo10086.Fragment;

import android.content.Intent;
import android.graphics.SurfaceTexture;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.huawei.hms.videokit.player.WisePlayer;
import com.huawei.hmsdemo10086.Activity.GoodsActivity;
import com.huawei.hmsdemo10086.Activity.MainActivity;
import com.huawei.hmsdemo10086.Activity.VideoActivity;
import com.huawei.hmsdemo10086.Adapter.VideoAdapter;
import com.huawei.hmsdemo10086.POJO.Result;
import com.huawei.hmsdemo10086.POJO.Video;
import com.huawei.hmsdemo10086.R;
import com.huawei.hmsdemo10086.VideoKitApplication;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class VideoFragment extends Fragment {

    View view;
    Result<List<Video>> result = null;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        view = inflater.inflate(R.layout.fragment_video, container, false);

        getVideo();
        while (result==null){
            Log.d("wait", "wait Video init");
        }
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.video_recycle_view);
        //recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(),2);
        recyclerView.setLayoutManager(gridLayoutManager);
        VideoAdapter adapter = new VideoAdapter(getActivity(),result.getData());

        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickLitener(new VideoAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(getActivity(),VideoActivity.class);
                intent.putExtra("video",result.getData().get(position));
                startActivity(intent);
            }

            @Override
            public void onCollection(View view, int position) {

            }
        });


        return view;
    }

    private void getVideo()
    {
        String url = "http://10.0.2.2:8443/api/listVideo";
        OkHttpClient mOkHttpClient = new OkHttpClient();

        Request request = new Request.Builder().url(url).get().build();

        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("测试", e+"");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                Gson gson = new Gson();
                String rep = response.body().string();
                System.out.println(rep);

                Type type = new TypeToken<Result<List<Video>>>(){}.getType();
                result = gson.fromJson(rep,type);
                for(int i = 0 ; i < result.getData().size() ; i++)
                {
                    System.out.println(result.getData().get(i).toString());
                }
            }
        });
    }

}